﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MixNP.Web.Classifieds.UserControls
{
    public partial class ListControl : System.Web.UI.UserControl
    {
        public enum ListControlTypeEnum
        {
            RadioButtonList, CheckBoxList, DropDownList
        }

        public ListControlTypeEnum ListControlType { get; set; }

        /// <summary>
        /// Override to force simple IDs all around
        /// </summary>
        public override string UniqueID
        {
            get
            {
                return this.ID;
            }
        }
        /// <summary>
        /// Override to force simple IDs all around
        /// </summary>
        public override string ClientID
        {
            get
            {
                return this.ID;
            }
        }

        public string ControlLabel { get; set; }
        public string CssClass { get; set; }
        public ListItemCollection ListItems { get; set; }
        public string InitialValue { get; set; }

        public string Note { get; set; }
        public bool? Enabled { get; set; }
        public bool Required { get; set; }
        public short TabIndex { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.AddListControl();
        }

        private void AddListControl()
        {
            this.SetDisabled();

            Literal1.Text = this.ControlLabel + ":";
            Literal1.ID = "Literal" + this.ID;

            if (!string.IsNullOrWhiteSpace(this.Note))
            {
                NoteLiteral.Text = this.Note;// +"<hr class=\"hr\" />";            
            }

            switch (this.ListControlType)
            {
                case ListControlTypeEnum.RadioButtonList:
                    RadioButtonList1.CssClass = this.CssClass;
                    RadioButtonList1.ID = this.ID;
                    RadioButtonList1.ClientIDMode = this.ClientIDMode;

                    if (this.TabIndex > 0)
                    {
                        RadioButtonList1.TabIndex = this.TabIndex;
                    }

                    if (this.ListItems.Count > 0)
                    {
                        foreach (ListItem item in this.ListItems)
                        {
                            RadioButtonList1.Items.Add(item);
                        }
                    }
                    //Label1.Attributes.Add("for", RadioButtonList1.ClientID);

                    DropDownList1.NamingContainer.Controls.Remove(DropDownList1);
                    CheckBoxList1.NamingContainer.Controls.Remove(CheckBoxList1);
                    break;
                case ListControlTypeEnum.DropDownList:
                    DropDownList1.CssClass = this.CssClass;
                    DropDownList1.ID = this.ID;
                    DropDownList1.ClientIDMode = this.ClientIDMode;

                    if (this.TabIndex > 0)
                    {
                        DropDownList1.TabIndex = this.TabIndex;
                    }

                    if (this.ListItems.Count > 0)
                    {
                        foreach (ListItem item in this.ListItems)
                        {
                            DropDownList1.Items.Add(item);
                        }
                    }

                    Label1.Attributes.Add("for", DropDownList1.ClientID);

                    CheckBoxList1.NamingContainer.Controls.Remove(CheckBoxList1);
                    RadioButtonList1.NamingContainer.Controls.Remove(RadioButtonList1);
                    break;
                case ListControlTypeEnum.CheckBoxList:
                    CheckBoxList1.CssClass = this.CssClass;
                    CheckBoxList1.ID = this.ID;
                    CheckBoxList1.ClientIDMode = this.ClientIDMode;

                    if (this.TabIndex > 0)
                    {
                        CheckBoxList1.TabIndex = this.TabIndex;
                    }

                    if (this.ListItems.Count > 0)
                    {
                        foreach (ListItem item in this.ListItems)
                        {
                            CheckBoxList1.Items.Add(item);
                        }
                    }

                    Label1.Attributes.Add("for", CheckBoxList1.ClientID);
                    DropDownList1.NamingContainer.Controls.Remove(DropDownList1);
                    RadioButtonList1.NamingContainer.Controls.Remove(RadioButtonList1);
                    break;
            }

            this.SetValidators();
            this.SetInitialValues();
        }

        private void SetInitialValues()
        {

            switch (this.ListControlType)
            {
                case ListControlTypeEnum.RadioButtonList:
                    RadioButtonList1.SelectedValue = this.InitialValue;
                    break;
                case ListControlTypeEnum.DropDownList:
                    DropDownList1.SelectedValue = this.InitialValue;
                    break;
                case ListControlTypeEnum.CheckBoxList:
                    string[] items = this.InitialValue.Split(',');

                    foreach (ListItem listItem in CheckBoxList1.Items)
                    {
                        foreach (string item in items)
                        {
                            if (listItem.Value.Trim().Equals(item.Trim()))
                            {
                                listItem.Selected = true;
                            }
                        }
                    }
                    break;
            }
        }

        private void SetDisabled()
        {
            if (this.Enabled != null)
            {
                switch (this.ListControlType)
                {
                    case ListControlTypeEnum.RadioButtonList:
                        RadioButtonList1.Enabled = (bool)this.Enabled;
                        break;
                    case ListControlTypeEnum.DropDownList:
                        DropDownList1.Enabled = (bool)this.Enabled;
                        break;
                    case ListControlTypeEnum.CheckBoxList:
                        CheckBoxList1.Enabled = (bool)this.Enabled;
                        break;
                }
            }
        }

        private void SetValidators()
        {
            this.SetRequiredFieldValidator();
        }

        private void SetRequiredFieldValidator()
        {
            RequiredFieldValidator1.ID = "Required" + this.ID;
            RequiredFieldValidator1.Enabled = this.Required;

            switch (this.ListControlType)
            {
                case ListControlTypeEnum.RadioButtonList:
                    RequiredFieldValidator1.ControlToValidate = RadioButtonList1.ID;
                    CheckBoxCustomValidator.NamingContainer.Controls.Remove(CheckBoxCustomValidator);
                    break;
                case ListControlTypeEnum.DropDownList:
                    RequiredFieldValidator1.ControlToValidate = DropDownList1.ID;
                    CheckBoxCustomValidator.NamingContainer.Controls.Remove(CheckBoxCustomValidator);
                    break;
                case ListControlTypeEnum.CheckBoxList:
                    RequiredFieldValidator1.NamingContainer.Controls.Remove(RequiredFieldValidator1);

                    CheckBoxCustomValidator.ID = "Required" + this.ID;
                    CheckBoxCustomValidator.Enabled = this.Required;

                    break;
            }
        }

        protected void CheckBoxCustomValidator_ServerValidation(object sender, ServerValidateEventArgs e)
        {
            e.IsValid = (CheckBoxList1.SelectedIndex != -1);
        }

    }
}